﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.AdminCP.Common;

namespace LinkCard.AdminCP.Deal
{
    public partial class category_new : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }
            if (!IsPostBack)
            {
                // Category Option 
                DataTable dt;
                dt = DataHelper.SelectTable("SELECT CategoryID, Text FROM Category Where Level < 2 ORDER BY Text", false);
                CategoryID.DataSource = dt;
                CategoryID.DataTextField = "Text";
                CategoryID.DataValueField = "CategoryID";
                CategoryID.DataBind();
                CategoryID.Items.Insert(0, "Nhóm mới");

                dt = DataHelper.SelectTable("Select count(*) as count from Category where Level = 1", false);
                int nCount = 1;
                if (dt.Rows.Count > 0) nCount = ConvertUtility.ToInt32(dt.Rows[0]["count"]) + 1;
                for (int i = 0; i < nCount; i++)
                {
                    CatePosition.Items.Insert(i, new ListItem((i + 1).ToString(), (i + 1).ToString()));
                }
            }
        }
        protected void btnADD_Click(object sender, EventArgs e)
        {
            int nCategoryParentID = ConvertUtility.ToInt32(CategoryID.SelectedValue);
            string sCategoryName = CategoryName.Value;
            string sCategoryText = "";
            //string sCategoryOrder = CategoryOrder.Value;

            int nDisplay = 0;
            if (Display.Checked) nDisplay = 1;
            int nCategoryLevel = 0;
            int nCatePosition = ConvertUtility.ToInt32(CatePosition.SelectedValue);

            string sSQL = "";

            if (nCategoryParentID == 0)
            {
                sCategoryText = sCategoryName;
                nCategoryLevel = 1;
            }
            else
            {
                sSQL = "select Text, Level from Category where CategoryID = " + nCategoryParentID;
                DataTable dt = DataHelper.SelectTable(sSQL, false);
                if (dt.Rows.Count > 0)
                {
                    sCategoryText = dt.Rows[0]["Text"].ToString() + " >> " + sCategoryName;
                    nCategoryLevel = Convert.ToByte(dt.Rows[0]["Level"]) + 1;
                }
            }

            sSQL = "INSERT INTO Category (Name, Text, ParentID, Level, IsDisplay, Position) VALUES ";
            sSQL += "(N'" + hash1way.SQLPatch(sCategoryName) + "',";
            sSQL += "N'" + hash1way.SQLPatch(sCategoryText) + "',";
            sSQL += nCategoryParentID + ",";
            sSQL += nCategoryLevel + ",";
            sSQL += nDisplay + ",";
            sSQL += nCatePosition + ")";

            DataHelper.ExecuteNonQuery(sSQL, true);

            Response.Redirect("category_list.aspx");

        }
    }
}